package com.eff.android.fragment.options;

import java.util.ArrayList;
import java.util.List;

import com.eff.android.R;
import com.eff.android.api.model.Level;
import com.eff.android.api.model.Setting;
import com.eff.android.api.model.Type;
import com.eff.android.fragment.EBaseFragment;
import com.eff.android.fragment.EFragmentFactory;
import com.eff.android.fragment.IFragmentCallback;
import com.eff.android.lib.ui.EButton;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;

public class EMainFragmentOptions extends EBaseFragment{

	public static final String TAG = EMainFragmentOptions.class.getSimpleName();
	
	private Fragment eMainOptionsSubjects;
	private IFragmentCallback callback;
	
	private EButton btnType;
	private EButton btnSubjects;
	private EButton btnLevel;
	private Setting settings;
	
	public EMainFragmentOptions(Bundle arguments) {
		super(arguments);
	}

	public static Bundle getArgs() {
		Bundle b = new Bundle();
		b.putString(TAG, TAG);
		return b;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		settings = (Setting) this.getArguments().getSerializable(Setting.TAG);
		//Log.d("Setting in EMainFragment", settings.getSubject().name());
		initFragment();
	}
	
	private void initFragment(){
		EFragmentFactory factory = EFragmentFactory.newInstance();
		eMainOptionsSubjects = factory.CreateFragment(EMainFragmentOptionsSubjects.TAG, this.getArguments());
	}

	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		
		View v = inflater.inflate(R.layout.e_frm_main_options, container, false);
		initViews(v);
		
		return v;
    }
	
	private class ButtonEventHandleHelper{
		private List<IButtonHandleHelper> strategyList = 
				new ArrayList<EMainFragmentOptions.IButtonHandleHelper>();
		
		public ButtonEventHandleHelper(EBaseFragment frm){
			this.strategyList.add(new LevelButtonEventProcess());
			this.strategyList.add(new TypeButtonEventProcess());
			this.strategyList.add(new SubjectButtonEventProcess(frm));
		}
		
		public void ProcessButtonEvent(int id){
			for (int i=0; i<strategyList.size(); i++){
				if (strategyList.get(i).getButtonId() == id)
					strategyList.get(i).ProcessEvent();
			}
		}
	}
	
	private abstract class IButtonHandleHelper{		
		private int buttonId;
		public abstract void ProcessEvent();
		
		protected int getButtonId() {
			return buttonId;
		}
		protected void setButtonId(int buttonId) {
			this.buttonId = buttonId;
		}
	}
	
	private class TypeButtonEventProcess extends IButtonHandleHelper{
		public TypeButtonEventProcess(){
			this.setButtonId(R.id.btnType);
		}
		@Override
		public void ProcessEvent() {
			if (btnType.getText() == Type.TypeType.Word.toString())
			{
				settings.setType(Type.TypeType.Phrase);
				btnType.setText(Type.TypeType.Phrase.toString());
			}
			else{
				settings.setType(Type.TypeType.Word);
				btnType.setText(Type.TypeType.Word.toString());
			}
		}		
	}
	
	private class LevelButtonEventProcess extends IButtonHandleHelper{
		public LevelButtonEventProcess(){
			this.setButtonId(R.id.btnLevel);
		}
		@Override
		public void ProcessEvent() {
			if (btnLevel.getText() == Level.LevelType.Easy.toString())
			{
				settings.setLevel(Level.LevelType.Normal);
				btnLevel.setText(Level.LevelType.Normal.toString());
			}
			else if (btnLevel.getText() == Level.LevelType.Normal.toString()){
				settings.setLevel(Level.LevelType.Hard);
				btnLevel.setText(Level.LevelType.Hard.toString());
			}
			else{
				settings.setLevel(Level.LevelType.Easy);
				btnLevel.setText(Level.LevelType.Easy.toString());
			}
		}		
	}
	
	private class SubjectButtonEventProcess extends IButtonHandleHelper{
		private EBaseFragment context;
		public SubjectButtonEventProcess(EBaseFragment frm){
			context = frm;
			this.setButtonId(R.id.btnSubjects);
		}
		@Override
		public void ProcessEvent() {
			IFragmentCallback callback = ((EMainFragmentOptions) context).getCallback();
			callback.gotoNextPage(eMainOptionsSubjects);
		}
	}
	
	public void ButtonEventHandler(View target){
		ButtonEventHandleHelper helper = new ButtonEventHandleHelper(this);
		helper.ProcessButtonEvent(target.getId());
	}

	private void initViews(View v) {
		btnSubjects = (EButton)v.findViewById(R.id.btnSubjects);
		btnSubjects.setText(settings.getSubjectName());
		btnType = (EButton) v.findViewById(R.id.btnType);
		btnType.setText(settings.getType().name());
		btnLevel = (EButton)v.findViewById(R.id.btnLevel);
		btnLevel.setText(settings.getLevel().name());
		
		SetButtonOnClickListener();
	}
	
	private void SetButtonOnClickListener(){
		btnSubjects.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View v) {
				ButtonEventHandler(v);
			}
		});
		btnType.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				ButtonEventHandler(v);
			}
		});
		btnLevel.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				ButtonEventHandler(v);
			}
		});
	}

	public IFragmentCallback getCallback() {
		return callback;
	}

	public void setCallback(IFragmentCallback callback) {
		this.callback = callback;
	}
}